using System;
using System.Collections;
using System.Collections.Generic;
using ConsoleCommand.Commands;
using ConsoleCommand.Utilities;

namespace ConsoleCommand{
    class Program{
        static void Main(string[] args){
            try{
                IDictionary<CommandParameter, string> parameters=ParameterParser.Parse(args);
                ICommand command=CommandFactory.Create(parameters);
                object result=command.Execute();
                Console.WriteLine(result);
                return;
            }catch(NotImplementedException){
                Usage();
            }catch (Exception ex){
                WriteError(ex);
            }
            Environment.Exit(-1);
        }

        static void WriteError(Exception ex) {
            Console.WriteLine("Unable to execute command.");
            Console.WriteLine(ex.Message);
            Console.WriteLine(Environment.NewLine);
        }
        
        private static void Usage()
        {
            Console.Write(@"
Usage:

ConsoleCommand ""Action"" [/ParameterName=Value[ ...]]

  ""Action""        The command action to take.
                    Possible values:[ 
                        BC|BackColor -- Set the background color of all following text
                        C|Color -- Set the background color of all following text
                        E|Extract|ExtractResource -- Extract an embedded resource from an assembly
                        H|Help -- Display Usage
                    ]
  ParameterName     The name of a parameter to pass to the Action Command
  Value             The value to pass with the named parameter to the Action Command

Example:

ConsoleCommand Color /Color=Red

");
        }

        private static void Usage(CommandType type) { 
            string usageFormat=String.Format("ConsoleCommand {0} {1}",type,"{0}");
            string parameters=String.Empty;
            switch(type){
                case CommandType.BC:
                case CommandType.BackColor:
                case CommandType.C:
                case CommandType.Color:
                    parameters = "/Color=[ConsoleColor Enum Value]";
                    break;
                case CommandType.E:
                case CommandType.Extract:
                case CommandType.ExtractResource:
                    parameters = "/Assembly=[Path to assembly] /Resource=[Name of resource to extract]";
                    break;
                case CommandType.L:
                case CommandType.List:
                case CommandType.ListResources:
                    parameters = "/Assembly=[Path to assembly]";
                    break;
                default:
                    parameters = "There doesn't seem to be anything here.";
                    break;
                
            }
            if (!String.IsNullOrEmpty(parameters)) Console.WriteLine(String.Format(usageFormat, parameters));
            else Usage();
        }
    }
}
